document.addEventListener('DOMContentLoaded', function() {
    // 检查会话存储中是否有当前模块ID
    const currentModule = sessionStorage.getItem('currentModule');
    if (!currentModule) {
        // 如果为空,默认显示数据概要
        document.getElementById('dashboard').classList.add('active');
        document.querySelector('a[href="#dashboard"]').parentElement.classList.add('active');
    } else {
        // 显示上次访问的模块
        document.querySelectorAll('.content-module').forEach(module => {
            module.classList.remove('active');
        });
        document.getElementById(currentModule).classList.add('active');
        document.querySelector(`a[href="#${currentModule}"]`).parentElement.classList.add('active');
    }
    // 侧边栏切换
    const sidebarItems = document.querySelectorAll('.sidebar-item a');
    sidebarItems.forEach(item => {
        item.addEventListener('click', function(e) {
            e.preventDefault();
            const targetId = this.getAttribute('href').substring(1);

            // 隐藏所有模块
            document.querySelectorAll('.content-module').forEach(module => {
                module.classList.remove('active');
            });

            // 显示目标模块
            const targetModule = document.getElementById(targetId);
            if (targetModule) {
                // 将当前模块ID存入会话存储
                sessionStorage.setItem('currentModule', targetId);
                targetModule.classList.add('active');
            }

            // 更新侧边栏状态
            sidebarItems.forEach(item => {
                item.parentElement.classList.remove('active');
            });
            this.parentElement.classList.add('active');
        });
    });


    // 初始化图表
    // 营收趋势图
    const revenueCtx = document.getElementById('revenueAnalysisChart').getContext('2d');
    new Chart(revenueCtx, {
        type: 'line',
        data: {
            labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
            datasets: [{
                label: '营收（万元）',
                data: [120, 150, 180, 220, 250, 286],
                borderColor: '#007bff',
                tension: 0.1,
                fill: false
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false
        }
    });

    // 收入来源分布图
    const revenuePieCtx = document.getElementById('revenuePieChart').getContext('2d');
    new Chart(revenuePieCtx, {
        type: 'doughnut',
        data: {
            labels: ['课程收入', '会员费', '器材租赁', '其他'],
            datasets: [{
                data: [65, 20, 10, 5],
                backgroundColor: ['#007bff', '#28a745', '#ffc107', '#dc3545']
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false
        }
    });

});

// 页面加载完成后移除loading动画
window.addEventListener('load', function() {

    const loading = document.getElementById('loading');
    loading.classList.add('close');
    setTimeout(function() {
        loading.remove();
    }, 600);

    $(document).ready(function(){
        var siteWelcome = document.getElementById('loading');
        siteWelcome.classList.add('close');
        setTimeout(function() {
            siteWelcome.remove();
        }, 600);
    });

    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();

    
});
  $(".header-mini-btn label svg").click(function(){
    if($(window).width() <= 768){
        if($("#mini-button").prop("checked") == true){
            $("#sidebar").removeClass("sidebar2");
        }else{
            $("#sidebar").addClass("sidebar2");
        }
     }
  });
  $(window).resize(function(){
    if($(window).width() > 768){
        $("#sidebar").removeClass("sidebar2");
        $("#mini-button").prop("checked",true);
     }
  });

